package com.yauchu.kegra.graph;

import java.util.ArrayList;
import java.util.Iterator;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yauchu.kegra.R;
import com.yauchu.kegra.myDragEventListener;
import com.yauchu.kegra.oelib.Operator;
import com.yauchu.net.LatexImageLoader;

public class GraphNode {

	// private Bitmap a;
	// private Bitmap b;
	// private Bitmap p;
	// private Bitmap defa;

	public ImageView imageView;
	public LinearLayout displayLayout;
	public Bitmap defaultBitmap;
	protected String genericStringRepresentation;
	protected String fullStringRepresentation;
	protected String colorCodedRepresentation;
	protected CurrentRepresentation currentRepresentation;
    private Boolean changed=false;
	private TextView textView;
	public Operator operator;
	protected GraphContainer graphContainer;

	// private LatexImageLoader Lil;

	public enum CurrentRepresentation {
		COLORCODED, FULLSTRING
	}

	public GraphNode(LinearLayout dL, Operator op) {
		Context context = dL.getContext();
		this.operator = op;
		this.displayLayout = dL;
		this.genericStringRepresentation = op.getGenericStringRepresentation();
		this.fullStringRepresentation = this.genericStringRepresentation;
		this.currentRepresentation = CurrentRepresentation.COLORCODED;
		this.imageView = new ImageView(context);

		LatexImageLoader Lil = new LatexImageLoader(this.imageView);
		Lil.execute(this.getCurrentRepresentation());

		this.textView = new TextView(context);
		textView.setText(this.genericStringRepresentation);

		this.displayLayout.addView(this.imageView);
		// this.displayLayout.addView(this.textView);
		/*
		 * a =
		 * BitmapFactory.decodeResource(imageView.getContext().getResources(),
		 * R.drawable.a); b =
		 * BitmapFactory.decodeResource(imageView.getContext().getResources(),
		 * R.drawable.b); p =
		 * BitmapFactory.decodeResource(imageView.getContext().getResources(),
		 * R.drawable.plus); defa =
		 * BitmapFactory.decodeResource(imageView.getContext
		 * ().getResources(),R.drawable.icon);
		 */
	}

	public void toggleRepresentation()
		{
		if (currentRepresentation==CurrentRepresentation.COLORCODED)
		{
			switchToFullRepresentation();
		}
		else
		{
			switchToColorCodedRepresentation();
		}
		
		}
	public void setChanged (Boolean setvalue)
	
	{
		
		changed=setvalue;
		
	}
	
	public Boolean getChanged()
	{
		return this.changed;
	}
	
	public void startRefreshWave()
	{
		this.changed=true;
		graphContainer.refreshUpFromThis(this);
		
	}
	
	private String getCurrentRepresentation()
	
	{
		if (currentRepresentation==CurrentRepresentation.COLORCODED)
		{
			return this.getColorCodedRepresentation();
		}
		else 
		{
			return getFullStringRepresentation();
		}
		
	}

	public void graphContainerInitialization(GraphContainer graphCon) {
		this.graphContainer = graphCon;
	}

	public void turnOnDragEventListeners() {
		imageView.setOnDragListener(new myDragEventListener(this));
		textView.setOnDragListener(new myDragEventListener(this));
		imageView.setOnClickListener(new SwitchView(this));
	}

	public void refreshCurrentRepresentation() {
		// this.fullStringRepresentation=getFullStringRepresentation();
		LatexImageLoader Lil = new LatexImageLoader(this.imageView);
		Lil.execute(getCurrentRepresentation());
		imageView.invalidate();

	}

	/*
	 * public void changeText (String newText) { textView.setText(newText);
	 * this.genericStringRepresentation=newText;
	 * 
	 * }
	 */

	public void setGenericRepresentation(String newGR) {
		this.genericStringRepresentation = newGR;
		operator = new Operator(newGR);

	}

	public String getGenericStringRepresentation() {
		return this.genericStringRepresentation;
	}

	public void switchToFullRepresentation() {
		this.currentRepresentation = CurrentRepresentation.FULLSTRING;
		refreshCurrentRepresentation();
	}

	public void switchToColorCodedRepresentation() {
		this.currentRepresentation = CurrentRepresentation.COLORCODED;
		refreshCurrentRepresentation();
	}

	public String getFullStringRepresentation() {
		this.fullStringRepresentation = operator
				.getExspression(this.graphContainer.getArguments(this));
		return this.fullStringRepresentation;
	}

	public String getColorCodedRepresentation() {
		ArrayList<String> colors = new ArrayList<String>();
		colors.add("{\\color{red}\\Box}");
		colors.add("{\\color{green}\\Box}");
		colors.add("{\\color{blue}\\Box}");
		colors.add("{\\color{yellow}\\Box}");
		colors.add("{\\color{magenta}\\Box}");
		colors.add("{\\color{orange}\\Box}");

		Iterator<String> colorsIterator = colors.iterator();
		return this.operator.getExspression(colorsIterator);
	}

}
